<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能化用例生成</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 300;
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .content {
            padding: 40px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 1.1em;
        }

        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-group input:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
            background: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-group textarea {
            min-height: 120px;
            resize: vertical;
        }

        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
            margin-top: 20px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }

        .loading.show {
            display: block;
        }

        .progress-bar {
            width: 100%;
            height: 6px;
            background: #e1e5e9;
            border-radius: 3px;
            overflow: hidden;
            margin: 20px 0;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            width: 0%;
            transition: width 0.3s ease;
            animation: progress 2s ease-in-out infinite;
        }

        @keyframes progress {
            0% { width: 0%; }
            50% { width: 70%; }
            100% { width: 100%; }
        }

        .loading-text {
            color: #667eea;
            font-weight: 600;
            margin-top: 10px;
        }

        .alert {
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            display: none;
        }

        .alert.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert.show {
            display: block;
        }

        .cases-list {
            margin-top: 30px;
            display: none;
        }

        .cases-list.show {
            display: block;
        }

        .case-item {
            background: #f8f9fa;
            border: 1px solid #e1e5e9;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.3s ease;
        }

        .case-item:hover {
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            transform: translateY(-2px);
        }

        .case-header {
            display: flex;
            justify-content: between;
            align-items: center;
            margin-bottom: 15px;
        }

        .case-id {
            background: #667eea;
            color: white;
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 0.9em;
            font-weight: 600;
        }

        .case-priority {
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 0.8em;
            font-weight: 600;
        }

        .priority-p1 { background: #dc3545; color: white; }
        .priority-p2 { background: #fd7e14; color: white; }
        .priority-p3 { background: #ffc107; color: #212529; }
        .priority-p4 { background: #6c757d; color: white; }

        .case-title {
            font-size: 1.2em;
            font-weight: 600;
            color: #333;
            margin-bottom: 10px;
        }

        .case-preconditions {
            background: #e3f2fd;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 15px;
            border-left: 4px solid #2196f3;
        }

        .case-steps {
            margin-top: 15px;
        }

        .step-item {
            background: white;
            border: 1px solid #e1e5e9;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 10px;
        }

        .step-number {
            background: #667eea;
            color: white;
            width: 25px;
            height: 25px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 0.9em;
            font-weight: 600;
            margin-right: 10px;
        }

        .step-content {
            display: inline-block;
            vertical-align: top;
            width: calc(100% - 40px);
        }

        .step-description {
            font-weight: 600;
            color: #333;
            margin-bottom: 5px;
        }

        .step-expected {
            color: #28a745;
            font-style: italic;
        }

        .view-cases-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
        }

        .view-cases-btn:hover {
            background: #218838;
            transform: translateY(-1px);
        }

        @media (max-width: 768px) {
            .content {
                padding: 20px;
            }
            
            .header h1 {
                font-size: 2em;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>测试用例生成系统</h1>
            <p>基于AI的智能测试用例生成工具</p>
        </div>
        
        <div class="content">
            <form id="testCaseForm">
                <div class="form-group">
                    <label for="projectName">项目名称</label>
                    <input type="text" id="projectName" name="projectName" placeholder="请输入项目名称">
                </div>
                
                <div class="form-group">
                    <label for="moduleName">模块名称</label>
                    <input type="text" id="moduleName" name="moduleName" placeholder="请输入模块名称">
                </div>
                
                <div class="form-group">
                    <label for="requirement">需求描述 *</label>
                    <textarea id="requirement" name="requirement" placeholder="请详细描述测试需求，包括功能点、业务流程等" required></textarea>
                </div>
                
                <button type="submit" class="btn" id="generateBtn">生成测试用例</button>
            </form>
            
            <div class="loading" id="loading">
                <div class="progress-bar">
                    <div class="progress-fill"></div>
                </div>
                <div class="loading-text">正在生成测试用例，请稍候...</div>
            </div>
            
            <div class="alert" id="alert"></div>
            
            <div class="cases-list" id="casesList"></div>
        </div>
    </div>

    <script>
        const form = document.getElementById('testCaseForm');
        const loading = document.getElementById('loading');
        const alert = document.getElementById('alert');
        const casesList = document.getElementById('casesList');
        const generateBtn = document.getElementById('generateBtn');

        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(form);
            const requirement = formData.get('requirement').trim();
            
            if (!requirement) {
                showAlert('请输入需求描述', 'error');
                return;
            }
            
            // 显示加载状态
            loading.classList.add('show');
            generateBtn.disabled = true;
            hideAlert();
            casesList.classList.remove('show');
            
            try {
                const response = await fetch('/api/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        requirement: requirement,
                        project_name: formData.get('projectName') || '',
                        module_name: formData.get('moduleName') || '',
                        test_types: ['functional'],
                        test_executors: ['qa'],
                        test_design_methods: ['use_case']
                    })
                });
                
                if (!response.ok) {
                    const errorData = await response.json();
                    throw new Error(errorData.detail?.message || errorData.detail || '生成失败');
                }
                
                const data = await response.json();
                
                if (data.test_cases && data.test_cases.length > 0) {
                    showAlert(`成功生成 ${data.test_cases.length} 个测试用例！`, 'success');
                    displayTestCases(data.test_cases);
                } else {
                    showAlert('未生成到有效的测试用例', 'error');
                }
                
            } catch (error) {
                console.error('生成测试用例失败:', error);
                showAlert(`生成失败: ${error.message}`, 'error');
            } finally {
                loading.classList.remove('show');
                generateBtn.disabled = false;
            }
        });

        function showAlert(message, type) {
            alert.textContent = message;
            alert.className = `alert ${type} show`;
        }

        function hideAlert() {
            alert.classList.remove('show');
        }

        function displayTestCases(cases) {
            casesList.innerHTML = '';
            
            cases.forEach((testCase, index) => {
                const caseElement = document.createElement('div');
                caseElement.className = 'case-item';
                
                const stepsHtml = testCase.steps ? testCase.steps.map((step, stepIndex) => `
                    <div class="step-item">
                        <span class="step-number">${step.step_number || stepIndex + 1}</span>
                        <div class="step-content">
                            <div class="step-description">${step.description || '步骤描述'}</div>
                            <div class="step-expected">预期结果: ${step.expected_result || '操作成功'}</div>
                        </div>
                    </div>
                `).join('') : '';
                
                caseElement.innerHTML = `
                    <div class="case-header">
                        <span class="case-id">${testCase.id || `TC${String(index + 1).padStart(3, '0')}`}</span>
                        <span class="case-priority priority-${testCase.priority?.toLowerCase() || 'p3'}">${testCase.priority || 'P3'}</span>
                    </div>
                    <div class="case-title">${testCase.title || `测试用例 ${index + 1}`}</div>
                    <div class="case-preconditions">
                        <strong>前置条件:</strong> ${testCase.preconditions || '系统正常运行'}
                    </div>
                    <div class="case-steps">
                        <strong>测试步骤:</strong>
                        ${stepsHtml}
                    </div>
                `;
                
                casesList.appendChild(caseElement);
            });
            
            casesList.classList.add('show');
        }
    </script>
</body>
</html>